grammar C_types;

options
{
	language    = C;
	ASTLabelType= pANTLR3_BASE_TREE;
}

@parser::includes{
#include <string>
#include <vector>
#include "util.h"
}

def returns [std::string definitions=""]
	:
	(
	((TYPEDECLARATION)? 
	t1=ID {definitions = "define.";          definitions+=(const char*) $t1.text->chars;}
	(
	'=' 
	(t2=ID {definitions+= ".variable.";       definitions+=(const char*) $t2.text->chars;}
	|t3=INT   {definitions+= ".constant.int.";   definitions+=(const char*) $t3.text->chars;}
	|t4=FLOAT {definitions+= ".constant.float."; definitions+=(const char*) $t4.text->chars;}
	|ASSIGNMENT{definitions+= ".assignment.";  definitions+=(const char*)$ASSIGNMENT.text->chars;}
	)
	)? 
	';')
	|
	(.*));

use returns [std::string uses=""]
	:
	(
	((TYPEDECLARATION)?
	t1=ID
	'='? 
	(t2=ID {uses+= "useof.";       uses+=(const char*) $t2.text->chars; uses+=".to."; uses+=(const char*) $t1.text->chars;}
	|ASSIGNMENT{uses+="useof.";	uses+=(const char*) $ASSIGNMENT.text->chars; uses+=".to."; uses+=(const char*) $t1.text->chars;}
	|INT
	|FLOAT
	)?
	';'? )
	|
	(ID|'('|')'|'{'|'}'|';'|':'|OPERATION)*);

testcriteria returns [std::vector<int> *vec]
@init   {
        $vec=new std::vector<int>();
        }
        :
        ('du<' t3=INT{vec->push_back(atoi((const char*)$t3.text->chars));}
        ','t4=INT{vec->push_back(atoi((const char*)$t4.text->chars));}
        '>'
        );

functionname returns [std::string func_name=""]
	:
	(
	t1=ID*{func_name+=(const char*) $t1.text->chars;}
	'.'
	t2=ID*
	);

linescope returns [int myscope]
	:
	(
	STRING
	'.scope='
	t1=INT{myscope=atoi((const char*)$t1.text->chars);}
	'.'
	STRING
	);
	
description returns [std::string des]
	:
	(t1=STRING
	'.scope='
	INT
	'.'
	t2=STRING{des=((const char*)$t2.text->chars);}
	);

path returns [std::vector<std::string> *vec]
@init   {
        $vec=new std::vector<std::string>();
	std::string temp_hex="";
        }
	:   
	(
        ('/'
        (ID|INT|'+'|'.')*
        )+
	FILENAME
        ':'?
        t0=INT{vec->push_back((const char*)$t0.text->chars);}
        '\n' WS*
        (t1=ID{temp_hex+=((const char*)$t1.text->chars);}  | t2=INT {temp_hex+=((const char*)$t2.text->chars);})*
        ':'{vec->push_back(temp_hex);}
        )
        ;

FILENAME:
        (ID+
        '.'
        (ID)*
        )
        ;

TYPEDECLARATION	:	('int'|'Int'|'INT'|'float'|'Float'|'String'|'STRING'|'string') //uppercase makes the identification case insensitive
	;

ASSIGNMENT:
	(
	(INT|FLOAT|ID)
	(OPERATION (INT|FLOAT|ID))+ 
	);

OPERATION: ('+'|'-'|'*'|'/')
	;

ID  :	('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;

INT :	'0'..'9'+
    ;

FLOAT
    :   ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
    |   '.' ('0'..'9')+ EXPONENT?
    |   ('0'..'9')+ EXPONENT
    ;

COMMENT
    :   '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
    |   '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
    ;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;

STRING
    :  '"' ( ESC_SEQ | ~('\\'|'"') )* '"'
    ;

CHAR:  '\'' ( ESC_SEQ | ~('\''|'\\') ) '\''
    ;

fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;

fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;

fragment
ESC_SEQ
    :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
    |   UNICODE_ESC
    |   OCTAL_ESC
    ;

fragment
OCTAL_ESC
    :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7')
    ;

fragment
UNICODE_ESC
    :   '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
    ;
